home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / mkdir.c < prev    next >
C/C++ Source or Header  |  1996-10-30  |  3KB  |  106 lines

  1. /* mkdir.c -- BSD compatible make directory function for System V
  2.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21.  
  22. #ifndef HAVE_MKDIR
  23.  
  24. #ifdef HAVE_SYS_TYPES_H
  25. #include <sys/types.h>
  26. #endif
  27. #include <sys/stat.h>
  28. #include <errno.h>
  29. #ifndef errno
  30. extern int errno;
  31. #endif
  32.  
  33. #ifdef STAT_MACROS_BROKEN
  34. #undef S_ISDIR
  35. #endif
  36.  
  37. #if !defined(S_ISDIR) && defined(S_IFDIR)
  38. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  39. #endif
  40.  
  41. #include "safe-stat.h"
  42.  
  43. /* mkdir adapted from GNU tar.  */
  44.  
  45. /* Make directory DPATH, with permission mode DMODE.
  46.  
  47.    Written by Robert Rother, Mariah Corporation, August 1985
  48.    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
  49.  
  50.    Severely hacked over by John Gilmore to make a 4.2BSD compatible
  51.    subroutine.    11Mar86; hoptoad!gnu
  52.  
  53.    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  54.    subroutine didn't return EEXIST.  It does now.  */
  55.  
  56. int
  57. mkdir (dpath, dmode)
  58.      char *dpath;
  59.      int dmode;
  60. {
  61.   int cpid, status;
  62.   struct stat statbuf;
  63.  
  64.   if (SAFE_STAT (dpath, &statbuf) == 0)
  65.     {
  66.       errno = EEXIST;        /* stat worked, so it already exists.  */
  67.       return -1;
  68.     }
  69.  
  70.   /* If stat fails for a reason other than non-existence, return error.  */
  71.   if (errno != ENOENT)
  72.     return -1;
  73.  
  74.   cpid = fork ();
  75.   switch (cpid)
  76.     {
  77.     case -1:            /* Cannot fork.  */
  78.       return -1;        /* errno is already set.  */
  79.  
  80.     case 0:            /* Child process.  */
  81.       /* Cheap hack to set mode of new directory.  Since this child
  82.      process is going away anyway, we zap its umask.
  83.      This won't suffice to set SUID, SGID, etc. on this
  84.      directory, so the parent process calls chmod afterward.  */
  85.       status = umask (0);    /* Get current umask.  */
  86.       umask (status | (0777 & ~dmode));    /* Set for mkdir.  */
  87.       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
  88.       _exit (1);
  89.  
  90.     default:            /* Parent process.  */
  91.       /* Wait for kid to finish.  */
  92.       while (wait (&status) != cpid)
  93.     /* Do nothing.  */ ;
  94.  
  95.       if (status & 0xFFFF)
  96.     {
  97.       /* /bin/mkdir failed.  */
  98.       errno = EIO;
  99.       return -1;
  100.     }
  101.       return chmod (dpath, dmode);
  102.     }
  103. }
  104.  
  105. #endif
  106.